import {
  tempAppointment
} from "../../utils/config";
import store from "../../utils/factory";

let that = null;
//获取应用实例
const app = getApp();

Component({
  pageLifetimes: {
    show() {
      if (typeof this.getTabBar === "function" && this.getTabBar()) {
        this.getTabBar().setData({
          selected: 1
        });
      }
    }
  },
  data: {
    data: {
      date: '',
      time: '',
      radioColor: "#EB6681",
      during: null,
      startTime: "",
      remark: "",
      name: "",
      phone: "",
      address: "",
      minTime: "",
      bed: "",
      check: false,
      room: ""
    },
  }, // 私有数据，可用于模板渲染
  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached: function () {
    that = this;
  }, // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready: function () {},
  methods: {
    pageScroll(e) {
      console.log(e.detail)
    },
    goSearch() {
      wx.navigateTo({
        url: `../search/search`
      });
    },
    checkAgreement(e) {
      this.setData({
        check: !this.data.check
      })
    },
    goAgreement() {
      wx.navigateTo({
        url: "../agreement/agreement"
      })
    },
    watchName(e) {
      this.setData({
        name: e.detail.value
      })
    },
    watchBed(e) {
      this.setData({
        bed: e.detail.value
      })
    },
    watchRoom(e) {
      this.setData({
        room: e.detail.value
      })
    },
    watchRemark(e) {
      this.setData({
        remark: e.detail.value
      })
    },
    watchPhone(e) {
      this.setData({
        phone: e.detail.value
      })
    },
    watchAddress(e) {
      this.setData({
        address: e.detail.value
      })
    },
    bindDateChange: function (e) {
      console.log(e)
      this.setData({
        startTime: e.detail.value
      })
    },
    radiochange: function (e) {
      this.setData({
        during: e.detail.value
      })
      console.log('radio发生change事件，携带的value值为：', e.detail.value)
    },
    onShareAppMessage() {
      return {
        title: "月嫂服务",
        path: '/pages/index/index'
      }
    },
    tempAppointment() {
      if (!this.data.name) {
        wx.showToast({
          icon: "none",
          title: "请填写联系人"
        })
        return false
      }

      if (!this.data.phone) {
        wx.showToast({
          icon: "none",
          title: "请填写手机号码"
        })
        return false
      }
      if (!this.data.address) {
        wx.showToast({
          icon: "none",
          title: "请填写联系地址"
        })
        return false
      }
      if (!this.data.startTime) {
        wx.showToast({
          icon: "none",
          title: "请填写上门时间"
        })
        return false
      }
      if (!this.data.room) {
        wx.showToast({
          icon: "none",
          title: "请填写病房号"
        })
        return false
      }
      if (!this.data.bed) {
        wx.showToast({
          icon: "none",
          title: "请填写病床号"
        })
        return false
      }
      wx.showModal({
        title: "温馨提示",
        content: "是否确认医院陪护信息",
        success: function (res) {
          if (res.confirm) {
            tempAppointment({
              address: that.data.address,
              name: that.data.name,
              phone: that.data.phone,
              remark: that.data.remark,
              room: that.data.room,
              bed: that.data.bed,
              startTime: that.data.startTime
            }).then(res => {
              that.setData({
                address: "",
                during: "",
                name: "",
                remark: "",
                phone: ""
              })
              wx.navigateTo({
                url: "../resultPage/resultPage"
              })
            })
          }
        }
      })
    }
  }
});